package com.sdkx.app.dialog.markdialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.sdkx.app.R;
import com.sdkx.app.ViewModel.MarkManager;

import java.util.ArrayList;

public class MarkTypeDialog extends Dialog {
    RecyclerView recyclerView;
    IMarkTypeListener iMarkTypeListener;
    public MarkTypeDialog(@NonNull Context context) {
        super(context);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setBackgroundDrawable(new ColorDrawable(getContext().getResources().getColor(android.R.color.transparent)));
        init();
    }

    public void init() {
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = getContext().getApplicationContext().getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        //lp.x=-80;
        lp.width = (int) (d.widthPixels ); // 高度设置为屏幕的0.6
        lp.height = (int) (d.heightPixels ); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
        View contentview = getLayoutInflater().inflate(R.layout.maketype_list, null);
        setContentView(contentview);
        recyclerView = (RecyclerView) contentview.findViewById(R.id.typelistview);
//        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.addItemDecoration( new TypeDecoration());
        MarkListAdapter adapter = new MarkListAdapter();
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }
    class TypeDecoration extends RecyclerView.ItemDecoration {
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            //设定底部边距为1px
            outRect.set(0, 0, 0, 1);
        }
    }
    public class MarkListAdapter extends RecyclerView.Adapter<MarkListAdapter.TypeViewHodler> {
        ArrayList<MarkManager.MarkShape> mTypes = MarkManager.getAllMarkShape();

        public MarkListAdapter() {
        }

        @NonNull
        @Override
        public TypeViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new TypeViewHodler(LayoutInflater.from(getContext()).inflate(R.layout.marktypeitem, null));
        }

        @Override
        public void onBindViewHolder(@NonNull TypeViewHodler holder, int position) {
            if (holder != null) {
                holder.initItem(position);
            }
        }

        @Override
        public int getItemCount() {
            return mTypes.size();
        }

        public class TypeViewHodler extends RecyclerView.ViewHolder {
            TextView typeName;
            ImageView typeImg;

            public TypeViewHodler(View itemView) {
                super(itemView);
                typeName = (TextView) itemView.findViewById(R.id.typename);
                typeImg = (ImageView) itemView.findViewById(R.id.typeimg);
            }

            public void initItem(int position) {
                MarkManager.MarkShape shape = mTypes.get(position);
                typeImg.setImageResource(shape.drawableId);
                typeName.setText(shape.shapName);
                itemView.setTag(position);
                itemView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        notifySelectListener((int)v.getTag())  ;
                    }
                });
            }
        }
    }
    public void setOnSelectListener(IMarkTypeListener listener){
        iMarkTypeListener = listener;
    }
    private void notifySelectListener(int pos){
        if(iMarkTypeListener != null){
            iMarkTypeListener.onSelect(pos);
        }
    }
    public interface  IMarkTypeListener{
        public void onSelect(int position);

    }
}
